package cn.gov.pbc.dream.controller;

import cn.gov.pbc.dream.common.Const;
import cn.gov.pbc.dream.common.ResponseCode;
import cn.gov.pbc.dream.common.ServerResponse;
import cn.gov.pbc.dream.pojo.UserSession;
import cn.gov.pbc.dream.pojo.query.bankfinancing.area.AreaQuery;
import cn.gov.pbc.dream.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/area/")
public class AreaController {

    @Autowired
    private IAreaService iAreaService;

    @GetMapping("getAllProvinceList.do")
    public ServerResponse getAllProvinceList(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iAreaService.getAllProvinceList();
    }

    @GetMapping("searchAreaInfo.do")
    public ServerResponse searchAreaInfo(HttpSession session,  AreaQuery areaQuery){
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iAreaService.searchArea(areaQuery);
    }

    @GetMapping("getCityByProvinceId.do/{provinceId}")
    public ServerResponse getCityByProvinceId(HttpSession session, @PathVariable Integer provinceId){
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iAreaService.getCityByProvinceId(provinceId);
    }
    @GetMapping("getDistrictByCityId.do/{cityId}")
    public ServerResponse getDistrictByCityId(HttpSession session, @PathVariable Integer cityId){
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iAreaService.getDistrictByCityId(cityId);
    }
}
